<?php
include_once (__ALIPATH__.'/com/alibaba/openapi/client/APIId.class.php');
include_once (__ALIPATH__.'/com/alibaba/openapi/client/APIRequest.class.php');
include_once (__ALIPATH__.'/com/alibaba/openapi/client/APIResponse.class.php');
include_once (__ALIPATH__.'/com/alibaba/openapi/client/SyncAPIClient.class.php');
include_once (__ALIPATH__.'/com/alibaba/openapi/client/entity/AuthorizationToken.class.php');
include_once (__ALIPATH__.'/com/alibaba/openapi/client/entity/ParentResult.class.php');
include_once (__ALIPATH__.'/com/alibaba/openapi/client/entity/ResponseStatus.class.php');
include_once (__ALIPATH__.'/com/alibaba/openapi/client/entity/ResponseWrapper.class.php');
include_once (__ALIPATH__.'/com/alibaba/openapi/client/policy/ClientPolicy.class.php');
include_once (__ALIPATH__.'/com/alibaba/openapi/client/policy/DataProtocol.class.php');
include_once (__ALIPATH__.'/com/alibaba/openapi/client/policy/RequestPolicy.class.php');

include_once (__ALIPATH__.'/com/alibaba/china/openapi/client/example/param/apiexample/ExampleFamilyGetParam.class.php');
include_once (__ALIPATH__.'/com/alibaba/china/openapi/client/example/param/apiexample/ExampleFamilyPostParam.class.php');
include_once (__ALIPATH__.'/com/alibaba/china/openapi/client/example/param/apiexample/ExampleFamilyGetResult.class.php');
include_once (__ALIPATH__.'/com/alibaba/china/openapi/client/example/param/apiexample/ExampleFamilyPostResult.class.php');

class ExampleFacade {
	private $serverHost = "gw.open.1688.com";
	private $httpPort = 80;
	private $httpsPort = 443;
	private $appKey;
	private $secKey;
	private $syncAPIClient;
    private $token;
    private $stdResult;
	public function setServerHost($serverHost = '') {
		$this->serverHost = $serverHost;
	}
	public function setHttpPort($httpPort) {
		$this->httpPort = $httpPort;
	}
	public function setHttpsPort($httpsPort) {
		$this->httpsPort = $httpsPort;
	}
	public function setAppKey($appKey) {
		$this->appKey = $appKey;
	}
	public function setSecKey($secKey) {
		$this->secKey = $secKey;
	}
    public function setToken($token) {
        $this->token = $token;
    }
	public function initClient() {
		$clientPolicy = new ClientPolicy ();
		$clientPolicy->appKey = $this->appKey;
		$clientPolicy->secKey = $this->secKey;
		$clientPolicy->httpPort = $this->httpPort;
		$clientPolicy->httpsPort = $this->httpsPort;
		$clientPolicy->serverHost = $this->serverHost;
		
		$this->syncAPIClient = new SyncAPIClient ( $clientPolicy->appKey, $clientPolicy->secKey);
	}
	public function getAPIClient() {
		if ($this->syncAPIClient == null) {
			$this->initClient ();
		}
		return $this->syncAPIClient;
	}

	public function ApiPost($api_name,$api_url,$param,$result_class) {
		$reqPolicy = new RequestPolicy ();
		$reqPolicy->httpMethod = "POST";
		$reqPolicy->needAuthorization = true;
		$reqPolicy->requestSendTimestamp = false;
		$reqPolicy->useHttps = false;
		$reqPolicy->useSignture = true;
		$reqPolicy->accessPrivateApi = false;
		
		$request = new APIRequest ();
		$apiId = new APIId ($api_url, $api_name, 1 );
		$request->apiId = $apiId;
        if(! empty($param)){
            foreach($param as $key=>$val){
                $request->addtionalParams[$key] = $val;
            }
        }
		$request->accessToken   = $this->token;
		$this->getAPIClient ()->send ($request, $result_class, $reqPolicy );
	}

    public function setStdResult($stdResult) {
        $this->stdResult = $stdResult;
    }
    
    public function getResult(){
		return json_decode(json_encode($this->stdResult),true);
    }
}
?>